﻿using OpenCvSharp;   //为了使用opencv
namespace JKYGL
{//需要安装两个拓展包：OpenCvSharp4和OpenCvSharp4.runtime.win
 // install-package OpenCvSharp4
 // install-package OpenCvSharp4.runtime.win
    public static  class OpenCvSharpHelper
    {
         public  static bool isHaveFace(string fname)
        {
            CascadeClassifier face = new CascadeClassifier("haarcascade_frontalface_alt.xml");
            Mat img1 = new Mat(fname, ImreadModes.Color);
            Mat pBkImage_gray = new Mat();
            Cv2.CvtColor(img1, pBkImage_gray, ColorConversionCodes.BGR2GRAY); //黑白化
            Cv2.EqualizeHist(pBkImage_gray, pBkImage_gray);//
            var facesDetected = face.DetectMultiScale(pBkImage_gray);
            if (facesDetected.Length>0) return true;else return false;    

        }
        public static bool isHaveFace(System.Drawing.Bitmap bm)
        {
            string tempPath =  Guid.NewGuid() + "_temp.PNG";
            bm.Save(tempPath, System.Drawing.Imaging.ImageFormat.Png);
            var re = isHaveFace(tempPath);
            File.Delete(tempPath);
            return re;


        }
    }
}
